package com.ruoyi.web.controller.system;

import com.alibaba.fastjson.JSONObject;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.WeightTableBeanDto;
import com.ruoyi.system.domain.WeightTableBean;
import com.ruoyi.system.service.WeightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * 体重操作
 */
@Slf4j
@RestController
@RequestMapping("/weight")
public class WeightController extends BaseController {

    @Autowired
    private WeightService weightService;

    /**
     * 通过
     * @param weightId
     * @return
     */
    @GetMapping("selectInfoById/{weightId}")
    public String selectInfoById(long weightId) {
        log.info("走到了 通过 id 获取体重  " + weightId);
        WeightTableBean weightTableBean = weightService.selectInfoById(weightId);
        String o = JSONObject.toJSONString(weightTableBean);
        return o;
    }

    /**
     * 查询一个用户所有的体重数据
     * @return
     */
    @GetMapping("/selectOneInfo")
    public AjaxResult selectOneInfo(Integer userId) {
        log.info("走到了 体重这里");
        List<WeightTableBean> demo01Configs = weightService.selectOneInfo(userId);
        return AjaxResult.success(demo01Configs);
    }

    /**
     * 导入体重记录
     * @param weightTableBeanDto
     * @return
     */
    @Log(title = "导入体重记录", businessType = BusinessType.INSERT)
    @PostMapping("/importWeightList")
    @ResponseBody
    public AjaxResult insertOneData(@RequestBody @Validated WeightTableBeanDto weightTableBeanDto) {
        if (weightTableBeanDto == null) {
            return error("参数为空");
        }
        Integer i = weightService.insertOneData(weightTableBeanDto);
        return AjaxResult.success("导入成功", i);
    }

}
